<template>
  <zk-modal v-model="visible" append-disabled :clickMaskClose="false">
    <div class="modal_confirm" :style="ConfirmStyle">
      <div class="modal_confirm_title">{{ options.title }}</div>
      <div class="modal_confirm_tips">{{ options.tips }}</div>
      <div class="modal_confirm_footer">
        <el-button round class="button_cancel" v-if="options.showCancel" @click.stop="handleCancel">{{
          options.cancelText
        }}</el-button>
        <el-button round class="button_confirm" v-if="options.showConfirm" @click.stop="handleConfirm">{{
          options.confirmText
        }}</el-button>
      </div>
    </div>
  </zk-modal>
</template>
<script setup lang="ts">
  import type { ZkModalConfirmInstance, OpenOptions } from './index'
  const visible = ref<boolean>(false)
  const DEF_OPTIONS: OpenOptions = {
    title: '是否退出账号？',
    tips: '',
    showConfirm: true,
    showCancel: true,
    confirmText: '确定',
    cancelText: '取消',
    confirm: () => {},
    cancel: () => {}
  }
  const options = ref<OpenOptions>({ ...DEF_OPTIONS })

  const ConfirmStyle = computed(() => {
    if (options.value?.tips != '') {
      return {
        minHeight: '224px',
        height: 'auto'
      }
    } else {
      return {
        height: '194px'
      }
    }
  })
  const open = (opts: OpenOptions) => {
    options.value = Object.assign({}, DEF_OPTIONS, opts)
    visible.value = true
  }
  const close = () => {
    visible.value = false
  }
  // 确认
  const handleConfirm = () => {
    try {
      if (options.value?.confirm) {
        options.value.confirm()
      }
      //   close()
    } catch (error) {
      console.error(error)
      throw error
    }
  }
  // 取消
  const handleCancel = () => {
    try {
      if (options.value?.cancel) {
        options.value.cancel()
      }
      close()
    } catch (error) {
      console.error(error)
      throw error
    }
  }
  defineExpose<ZkModalConfirmInstance>({
    open,
    close
  })
</script>
<style lang="scss" scoped>
  .modal_confirm {
    width: 277px;
    height: 194px;
    border-radius: 20px;
    background: rgba(255, 255, 255, 1);
    display: flex;
    flex-direction: column;
    padding: 55px 20px 38px;
    .modal_confirm_title {
      height: 61px;
      line-height: 24px;
      color: rgba(45, 49, 68, 1);
      font-family: Noto Sans SC;
      font-size: 18px;
      font-weight: 700;
      letter-spacing: 0%;
      text-align: center;
      padding-bottom: 20px;
    }
    .modal_confirm_tips {
      // overflow: hidden;
      // text-overflow: ellipsis;
      // white-space: nowrap;
      word-break: break-all;
      color: rgb(94, 101, 136);
      font-family: Noto Sans SC;
      font-size: 12px;
      font-weight: 700;
      letter-spacing: 0%;
      text-align: left;
      padding-bottom: 20px;
    }
    .modal_confirm_footer {
      display: flex;
      justify-content: center;
      align-items: center;
      .button {
        width: 98px;
        height: 40px;
        border-radius: 25px;
        border: none;
        font-family: Noto Sans SC;
        font-size: 16px;
        font-weight: 700;
        line-height: 100%;
        letter-spacing: 0%;
        text-align: center;
      }
      .button_confirm {
        @extend .button;
        background: rgba(176, 195, 255, 0.2);
        color: rgba(74, 114, 255, 1);
      }
      .button_cancel {
        @extend .button;
        background: rgba(171, 171, 171, 0.2);
        color: rgba(157, 157, 157, 1);
      }
    }
  }
</style>
